package com.app.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.entity.ResidentialGuarantee;
import com.app.entity.User;
import com.app.exception.ValidateException;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.UserGuaranteeService;

@Controller
@RequestMapping("/userGuarantee")
public class UserGuaranteeController extends BaseController{

	
	@Autowired
	private UserGuaranteeService userGuaranteeService;
	
	@RequestMapping(value = "/insertUserGuarantee")
    public void insertUserGuarantee(ResidentialGuarantee oResidentialGuarantee,
                           HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        //ResidentialGuarantee oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
        try {
        	userResponseVO = userGuaranteeService.insertUserGuarantee(oResidentialGuarantee);
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }
	@RequestMapping(value = "/updateUserGuarantee")
	public void updateUserGuarantee(ResidentialGuarantee oResidentialGuarantee,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialGuarantee oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
		try {
			userResponseVO = userGuaranteeService.updateUserGuarantee(oResidentialGuarantee);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}
	@RequestMapping(value = "/deleteUserGuarantee")
	public void deleteUserGuarantee(ResidentialGuarantee oResidentialGuarantee,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialGuarantee oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
		try {
			userResponseVO = userGuaranteeService.deleteUserGuarantee(oResidentialGuarantee);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}
	
	@RequestMapping("/selectUserGuaranteeList")
    public void selectUserGuaranteeList(ResidentialGuarantee oResidentialGuarantee,
                                        @RequestParam("currentPage") int currentPage,
                                        @RequestParam("pagesize") int pagesize,
                              HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<ResidentialGuarantee> returnResultResponseVO = new ReturnResultResponseVO<ResidentialGuarantee>();
        Map<String,Object> map = new HashMap<String, Object>();
        try {
        	//ResidentialGuarantee oResidentialGuarantee = new ResidentialGuarantee();
        	//oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
            returnResultResponseVO = userGuaranteeService.selectUserGuaranteeList(oResidentialGuarantee,currentPage, pagesize);
            returnResultResponseVO.setStatue(true);
            //map.put("count", 10);
            //map.put("data",returnResultResponseVO.getData());
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
       
        writeJSON(response, returnResultResponseVO);
    }
	@RequestMapping("/selectUserGuaranteePageList")
	@ResponseBody
	public JSONObject selectUserGuaranteePageList(ResidentialGuarantee oResidentialGuarantee,@RequestParam("page") int currentPage,@RequestParam("limit") int pagesize,
			HttpServletResponse response,HttpServletRequest request) throws IOException {
		ReturnResultResponseVO<ResidentialGuarantee> returnResultResponseVO = new ReturnResultResponseVO<ResidentialGuarantee>();
		Map<String,Object> map = new HashMap<String, Object>();
		JSONObject jsonObject = new JSONObject();  
		try {
			//ResidentialGuarantee oResidentialGuarantee = new ResidentialGuarantee();
			//oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
			oResidentialGuarantee.setUserName((String)request.getSession().getAttribute("userLogin"));
			returnResultResponseVO = userGuaranteeService.selectUserGuaranteePageList(oResidentialGuarantee,currentPage, pagesize);
			returnResultResponseVO.setStatue(true);
			map.put("count", 10);
			jsonObject.put("count", returnResultResponseVO.getPage().getPagesize());  
			jsonObject.put("data", returnResultResponseVO.getData());  
			map.put("data",returnResultResponseVO.getData());
		} catch (ValidationException e) {
			returnResultResponseVO.setStatue(false);
		}
		jsonObject.put("code", 0);  
		jsonObject.put("msg", "");  
		//System.out.println("json:----" + jsonObject.toString());  
		return jsonObject; 
		//writeJSON(response, map);
	}
	@RequestMapping("/baoxiuFormPage")
	public String baoxiuFormPage(){
		return "baoxiuForm";
	}
	@RequestMapping("/user")
	public String user(){
		return "index";
	}
}
